#python modules
import logging
import os
import datetime

#web modules
import webapp2
import jinja2

#google modules
from google.appengine.api import users

#application modules
from config import config
from models.models import *

jinja_environment = jinja2.Environment(autoescape=True,loader=jinja2.FileSystemLoader(os.path.join(os.path.dirname(__file__), '../views')))

class MonthHandler(webapp2.RequestHandler):
    def get(self):
        user = users.get_current_user()
        system_date = datetime.datetime.now().strftime("%d-%m-%Y")
        budgetService = BudgetService()
        
        id_year = int(config.year)
        budget = budgetService.get_by_year(id_year,user)

        ###FIRST LOGIN WORK-AROUND###
        if budget == None:
            budgetService = BudgetService()
            budgetService.initialize(config.year,user)
            budget = budgetService.get_by_year(id_year,user)
        
        summary = budget.total_by_category_month()
        
        # We are using the template module to output the page.
        template = jinja_environment.get_template('summary_month.html')		
        template_values = {'username':user.nickname(),
                           'url': users.create_logout_url("/"),
                           'system_date':system_date,
                           'summary':summary}
        self.response.out.write(template.render(template_values))
        

class BalancesheetHandler(webapp2.RequestHandler):
    def get(self):
        user = users.get_current_user()
        system_date = datetime.datetime.now().strftime("%d-%m-%Y")
        
        # We are using the template module to output the page.
        template = jinja_environment.get_template('summary_balance.html')		
        template_values = {'username':user.nickname(),
                           'url': users.create_logout_url("/"),
                           'system_date':system_date
                           }
        self.response.out.write(template.render(template_values))
        

class TypeHandler(webapp2.RequestHandler):
    def get(self):
        user = users.get_current_user()
        system_date = datetime.datetime.now().strftime("%d-%m-%Y")

        # We are using the template module to output the page.
        template = jinja_environment.get_template('summary_type.html')		
        template_values = {'username':user.nickname(),
                           'url': users.create_logout_url("/"),
                           'system_date':system_date,
                           }
        self.response.out.write(template.render(template_values))
        

